﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

public partial class GiaoDienClient_TrangThongTinChiTietDiem : System.Web.UI.Page
{
  
    SoLienLacDataContext db = new SoLienLacDataContext();
    clsThaoTac clstt = new clsThaoTac();
    string mahs;
    string tenhs;
    string manam;
    string malop;
    protected void Page_Load(object sender, EventArgs e)
    {
        mahs=Request.QueryString["MaHS"];
       
       // Response.Write(tenhs);
        manam = Request.QueryString["MaNam"];
        if (manam == null)
        { manam = "1"; }
        if (mahs == null)
        {
            if (Session["Username"].ToString() != null)
            {
                mahs = Session["Username"].ToString();
            }
        }
        var c = from p in db.HocSinhs where p.MaHS == mahs select p.TenHS;
        tenhs = c.First().ToString();
        LoadThongTinHS(mahs);
        var lp = from p in db.Lop_HocSinhs where p.MaHS == mahs && p.MaNam == int.Parse(manam) select new { p.MaLop,p.LopHoc.TenLop };
        malop = lp.First().MaLop.ToString();
        if (!IsPostBack)
        {
           
            lblMaHS.Text = mahs;
            lblTenHS.Text = tenhs;
            lblLop.Text ="-"+ lp.First().TenLop;
            if (rdoKyI.Checked == true)
            {
                clstt.LoadDiemChiTiet(int.Parse(manam), 1, lblMaHS.Text, grDiemChiTiet, lblDTB, lblHanhKiem, lblHocLuc);
            }
            //if (rdoKyII.Checked == true)
            //{
            //    clstt.LoadDiemChiTiet(int.Parse(manam), 2, lblMaHS.Text, grDiemChiTiet, lblDTB, lblHanhKiem, lblHocLuc);
            //}
            if (rdoHocKyI.Checked == true)
            {
                LoadThongTinVP(int.Parse(manam), 1, lblMaHS.Text);
            }
            //if (rdoHocKyII.Checked == true)
            //{
            //    LoadThongTinVP(int.Parse(manam), 2, mahs);
            //}
            //if (rdoToanNam.Checked == true)
            //{
            //    LoadLoiCN(int.Parse(manam), mahs);
            //}
            LoadGVCN(int.Parse(manam), int.Parse(malop));
            LoadGridGiaoVien(int.Parse(manam), int.Parse(malop));
            LoadDatalistYKien();
            //LoadDatalistYKienGV();
        }
    }
    private void LoadGVCN(int manam, int malop)
    {
        var c = from p in db.Lop_Bans
                where p.MaNam == manam && p.MaLop == malop
                select new { p.CanBoGiaoVien.TenCanBo,p.MaCanBoGV, p.CanBoGiaoVien.SDT, p.CanBoGiaoVien.DiaChi, p.CanBoGiaoVien.Email };
        lblMaGVCN.Text = c.First().MaCanBoGV;
        lblTenGV.Text = c.First().TenCanBo;
        lblSDT.Text = c.First().SDT;
        lblDiaChi.Text = c.First().DiaChi;
        lblEmail.Text = c.First().Email;

    }
    private void LoadGridGiaoVien(int manam,int malop)
    {
        var c = from p in db.GiaoVien_Lop_Mons
                where p.MaNam == manam && p.MaLop == malop
                select new { p.CanBoGiaoVien.TenCanBo,p.MaCanBoGV, p.MonHoc.TenMon, p.CanBoGiaoVien.SDT, p.CanBoGiaoVien.Email, p.CanBoGiaoVien.DiaChi };
        gridGiaoVien.DataSource = c;
        gridGiaoVien.DataBind();
    }
    protected void rdoKyI_CheckedChanged(object sender, EventArgs e)
    {
        if (rdoKyI.Checked == true)
        {
            clstt.LoadDiemChiTiet(int.Parse(manam), 1, mahs, grDiemChiTiet, lblDTB, lblHanhKiem, lblHocLuc);
            MultiView1.ActiveViewIndex = 0;
        }
       
    }
    protected void rdoKyII_CheckedChanged(object sender, EventArgs e)
    {
        
    }
    protected void rdoKyII_CheckedChanged1(object sender, EventArgs e)
    {
        if (rdoKyII.Checked == true)
        {
            clstt.LoadDiemChiTiet(int.Parse(manam), 2, mahs, grDiemChiTiet, lblDTB, lblHanhKiem, lblHocLuc);
            MultiView1.ActiveViewIndex = 0;
        }
        
    }
    private void LoadThongTinHS(string mahs)
    {
        var c = from p in db.HocSinhs
                where p.MaHS == mahs
                select new {p.QueQuan,p.NgaySinh,p.GioiTinh };
        lblTen.Text = tenhs;
        lblMa.Text = mahs;
        lblGioi.Text = c.First().GioiTinh;
        lblNgaySinh.Text = c.First().NgaySinh.ToShortDateString();
        lblQueQuan.Text = c.First().QueQuan;
    }
    private void LoadThongTinVP(int manam,int maky,string mahs)
    {
        var c = from p in db.ViPhams
                where p.MaNam == manam && p.MaKy == maky && p.MaHS == mahs
                select new { p.LoiViPham.TenLoiVP, p.NgayVP, p.SoLanVP };
        grvLoiVP.DataSource = c;
        grvLoiVP.DataBind();
        var cp = from p in db.ViPhams
                 where p.MaNam == manam && p.MaKy == maky && p.MaHS == mahs && p.LoiViPham.TenLoiVP == "Nghỉ học có phép"
                 select new {p.MaLoiVP};
        lblNghiCP.Text = cp.Count().ToString();
        var kp = from p in db.ViPhams
                 where p.MaNam == manam && p.MaKy == maky && p.MaHS == mahs && p.LoiViPham.TenLoiVP == "Nghỉ học không phép"
                 select new { p.MaLoiVP };
        lblNghiKP.Text = kp.Count().ToString();
        if (lblNghiCP.Text != "" || lblNghiKP.Text != "")
        {
            lblTongNghi.Text = (int.Parse(lblNghiCP.Text) + int.Parse(lblNghiKP.Text)).ToString();
        }
    }
    private void LoadLoiCN(int manam, string mahs)
    {
        var c = from p in db.ViPhams
                where p.MaNam == manam && p.MaHS == mahs
                select new { p.LoiViPham.TenLoiVP, p.NgayVP, p.SoLanVP };
        grvLoiVP.DataSource = c;
        grvLoiVP.DataBind();
        var cp = from p in db.ViPhams
                 where p.MaNam == manam && p.MaHS == mahs && p.LoiViPham.TenLoiVP == "Nghỉ học có phép"
                select new { p.MaLoiVP};
        lblNghiCP.Text = cp.Count().ToString();
        var kp = from p in db.ViPhams
                 where p.MaNam == manam && p.MaHS == mahs && p.LoiViPham.TenLoiVP == "Nghỉ học không phép"
                select new { p.MaLoiVP };
        lblNghiKP.Text = kp.Count().ToString();
        if (lblNghiCP.Text != "" || lblNghiKP.Text != "")
        {
            lblTongNghi.Text = (int.Parse(lblNghiCP.Text) + int.Parse(lblNghiKP.Text)).ToString();
        }
    }

    protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
    {
        if (rdoHocKyI.Checked == true)
        {
            LoadThongTinVP(int.Parse(manam), 1, mahs);
        }
    }
    protected void rdoHocKyII_CheckedChanged(object sender, EventArgs e)
    {
        if (rdoHocKyII.Checked == true)
        {
            LoadThongTinVP(int.Parse(manam), 2, mahs);
        }
        
    }
    protected void rdoToanNam_CheckedChanged(object sender, EventArgs e)
    {
        if (rdoToanNam.Checked == true)
        {
            LoadLoiCN(int.Parse(manam), mahs);
        }
        
    }
    protected void rdoCaNam_CheckedChanged(object sender, EventArgs e)
    {
        MultiView1.ActiveViewIndex = 1;
        clstt.LoadGridCaNam(int.Parse(manam),mahs,grvDTBCaNam);
        var c = from p in db.KetQuaCaNams where p.MaNam == int.Parse(manam) && p.MaHS == mahs select new { p.HocLuc, p.DiemTKCN };
        if (c.First().DiemTKCN != null)
        {
            lblDTB.Text = c.First().DiemTKCN.ToString();
        }
        if (c.First().DiemTKCN == null)
        {
            lblDTB.Text = "";
        }
        if (c.First().HocLuc != "")
        {
            lblHocLuc.Text = c.First().HocLuc;
        }
        if (c.First().HocLuc != "")
        {
            lblHocLuc.Text = "";
        }
        var c1 = from p in db.KetQuaTongHops
                where p.MaNam == int.Parse(manam) && p.MaKy == 2 && p.MaHS == mahs
                select p.HanhKiem;
        if (c1.Count() != 0)
        {
            if (lblDTB.Text == "")
            {
                lblHanhKiem.Text = "";
            }
            if (lblDTB.Text != "")
            {

                if (c1.First() != null)
                {
                    lblHanhKiem.Text = c1.First().ToString();
                }
                else lblHanhKiem.Text = "";
            }
        }
        else lblHanhKiem.Text = "";

       
        
        //var c=from p in db.KetQuaTongHops
        //      where 
    }

    protected void imbBack_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("TrangTimMaHS.aspx");
    }
    protected void ibtYKien_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("~/GiaoDienClient/TrangYKienPhuHuynh.aspx?MaCanBoGV=" + this.lblMaGVCN.Text+"&TenCanBoGV="+this.lblTenGV.Text);
    }
    private void LoadDatalistYKien()
    {
        if (Session["Username"].ToString() != null || Session["Username"].ToString() != "")
        {
            var c = from p in db.YKienPhuHuynhs
                    where p.MaHS == Session["Username"].ToString()
                    select new { p.TieuDe, p.NoiDungYKien, p.CanBoGiaoVien.TenCanBo, p.MaTin };
            dtlYKien.DataSource = c;
            dtlYKien.DataBind();
            //for (int i = 0; i < dtlYKien.Items.Count; i++)
            //{

            //}

        }
    }
    //private void LoadDatalistYKienGV(int matin)
    //{
    //    var c = from p in db.YKienPhuHuynhs
    //            where p.MaHS == Session["Username"].ToString()&&p.MaTin==matin
    //            select new { p.TieuDe, p.NoiDungPhanHoi, p.HocSinh.TenHS };
    //    DataList dtl = dtlYKien.FindControl(" datalistGV") as DataList;
    //    dtl.DataSource = c;
    //    dtl.DataBind();
        
    //}
    protected void dtlYKien_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        Label matin = e.Item.FindControl("lblMaTin1") as Label;
        int matin1 = int.Parse(matin.Text);
        DataList dtl = e.Item.FindControl("dtlYKienGV") as DataList;
        var c1 = from p in db.YKienPhuHuynhs
                 where p.MaHS == Session["Username"].ToString() && p.MaTin == matin1&& p.NoiDungPhanHoi!=null
                 select new { p.TieuDe, p.NoiDungPhanHoi, p.HocSinh.TenHS };
        dtl.DataSource = c1;
        dtl.DataBind();
    }
}